package com.sh.shanhuo.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sh.shanhuo.R;
import com.sh.shanhuo.adapter.PushDetailAdapter;
import com.sh.shanhuo.bean.BaseBean;
import com.sh.shanhuo.bean.PushDetailListBean;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.utils.Tools;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.viewpager2.widget.ViewPager2;
import okhttp3.Call;

public class RecommendationInfoActivity extends BaseActivity {
    private String id;
    private String webName;
    private String logoUrl;
    private String introduce;
    private String webUrl;
    private String abstraction;
    private PushDetailAdapter detailAdapter;
    private int siteId;
    private boolean hasList;
    private String userName, pushId;
    private List<PushDetailListBean.DataBean> dataBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommendation_info;
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "ClickableViewAccessibility"})
    @Override
    protected void initView() {
        super.initView();
        getBundle();
        ImageView back = findViewById(R.id.back);
        TextView title = findViewById(R.id.title);
        TextView editorTips = findViewById(R.id.editorTips);
        ViewPager2 pager2 = findViewById(R.id.viewPager);
        detailAdapter = new PushDetailAdapter(RecommendationInfoActivity.this, null);
        pager2.setAdapter(detailAdapter);
        pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }
        });
        back.setOnClickListener(v -> finish());
        if (hasList) {
            editorTips.setVisibility(View.VISIBLE);
            title.setText("推荐详情");
            getPushDetailById();
        } else {
            editorTips.setVisibility(View.GONE);
            title.setText("详情");
            PushDetailListBean.DataBean dataBean = new PushDetailListBean.DataBean();
            dataBean.setId(id);
            dataBean.setWebName(webName);
            dataBean.setWebUrl(webUrl);
            dataBean.setLogoUrl(logoUrl);
            dataBean.setIntroduce(introduce);
            dataBean.setAbstraction(abstraction);
            List<PushDetailListBean.DataBean> dataBeans = new ArrayList<>();
            dataBeans.add(dataBean);
            detailAdapter.setNewData(dataBeans);
        }
        readPush();
    }

    private void getPushDetailById() {
        onShowLoading();
        Map<String, String> params = new HashMap<>();
        params.put("id", siteId + "");
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.PUSH_DETAIL, params, new GsonObjectCallback<PushDetailListBean>() {
            @Override
            public void onUi(PushDetailListBean pushDetailListBean) {
                onDismissLoading();
                if (pushDetailListBean.getCode() == 200 && "操作成功".equals(pushDetailListBean.getMsg())) {
                    dataBeans = pushDetailListBean.getData();
                    detailAdapter.setNewData(dataBeans);
                }
            }

            @Override
            public void onFailed(Call call, IOException e) {
                onDismissLoading();
            }
        });
    }

    private void getBundle() {
        Intent intent = getIntent();
        if (intent == null)
            return;
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        id = bundle.getString("id", "");
        webName = bundle.getString("webName", "");
        logoUrl = bundle.getString("logoUrl", "");
        introduce = bundle.getString("introduce", "");
        webUrl = bundle.getString("webUrl", "");
        abstraction = bundle.getString("abstraction", "");

        siteId = bundle.getInt("siteId", 0);
        hasList = bundle.getBoolean("hasList", false);
        userName = bundle.getString("userName", "");
        pushId = bundle.getString("pushId", "");
    }

    private void readPush() {
        Map<String, String> params = new HashMap<>();
        if (TextUtils.isEmpty(userName)) {
            params.put("uuid", Tools.getUniquePsuedoID());
        } else {
            params.put("userName", userName);
        }
        params.put("pushId", pushId);
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.READ_PUSH, params, new GsonObjectCallback<BaseBean>() {
            @Override
            public void onUi(BaseBean baseBean) {

            }

            @Override
            public void onFailed(Call call, IOException e) {

            }
        });
    }
}
